package cn.com.common;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

public class EncryptUtil {
	/**
	 * Encrypt MD5 or SHA
	 * @param inputSTr
	 * @param eType  'MD5' or 'SHA'
	 * @return encrypt result
	 */
	public static String EncryptMd5OrSHA(String inputSTr, String eType) {
		String result = null;
		try {
			MessageDigest md5 = MessageDigest.getInstance(eType);
			byte[] digestBt = md5.digest(inputSTr.getBytes());
			StringBuffer stringBuffer = new StringBuffer();
			for (byte bitem : digestBt) {
				int item = bitem & 0xff;
				if (item < 16) {
					stringBuffer.append(0);
				}
				stringBuffer.append(Integer.toHexString(item));
			}
			result = stringBuffer.toString();
		} catch (NoSuchAlgorithmException e) {
			//e.printStackTrace();
		}
		return result;
	}
	/**
	 * encode base64
	 * @param inputStr
	 * @return encode result
	 */
	public static String EncodeBase64(String inputStr) {
		String result=null;
		if(inputStr==null||inputStr.isEmpty()){
			return result;
		}
		 byte[] encodedBytes=Base64.getEncoder().encode(inputStr.getBytes());
		 result=new String(encodedBytes);
		 return result;
	}
	/**
	 * decode base64
	 * @param inputStr
	 * @return decode result
	 */
	public static String DecodeBase64(String inputStr) {
		String result=null;
		if(inputStr==null||inputStr.isEmpty()){
			return result;
		}
		 byte[] decodedBytes=Base64.getDecoder().decode(inputStr.getBytes());
		 result=new String(decodedBytes);
		 return result;
	}
}
